package lily.com.app;

import java.util.ArrayList;
import android.content.ContentValues;
import android.database.Cursor;
import android.util.Log;

public class TaskDao {

	interface TaskDaoFields {
		String COL_NAME_TID = "tid";
		String COL_NAME_TITLE = "title";
		String COL_NAME_TEXT = "text";
		String COL_NAME_STATUS = "status";
	}

	private static final String TABLE_NAME_TASKS = "tasks";
	private static final String SQL_CREATE_TABLE_TASKS = "CREATE TABLE IF NOT EXISTS tasks (tid INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL, text TEXT, status INTEGER);";
	private static final String TABLE_NAME_ORDERS = "orders";
	private static final String SQL_CREATE_TABLE_ORDERS = "CREATE TABLE IF NOT EXISTS orders (rid INTEGER PRIMARY KEY AUTOINCREMENT, tid INTEGER UNIQUE);";
	private static final String SQL_DROP_TABLE_ORDERS = "DROP TABLE IF EXISTS orders;";
	
	// TODO not appropriate to creatTable in constructor
	public TaskDao() {
		createTableTasks();
		createTableOrders();
	}

	public boolean createTableTasks() {
		return SQLiteManager.execute(SQL_CREATE_TABLE_TASKS);
	}

	public boolean createTableOrders() {
		return SQLiteManager.execute(SQL_CREATE_TABLE_ORDERS);
	}
	
	public boolean dropTableOrders(){
		return SQLiteManager.execute(SQL_DROP_TABLE_ORDERS);
	}

	public TaskDomain selectTask(int tid) {
		Cursor cursor = SQLiteManager
				.query(TABLE_NAME_TASKS, new String[] {
						TaskDaoFields.COL_NAME_TID,
						TaskDaoFields.COL_NAME_TITLE,
						TaskDaoFields.COL_NAME_TEXT,
						TaskDaoFields.COL_NAME_STATUS }, "tid=?",
						new String[] { String.valueOf(tid) }, null, null, null);
		if (null == cursor)
			return null;

		TaskDomain taskDomain = null;

		if (cursor.moveToNext()) {
			taskDomain = new TaskDomain();
			taskDomain.setTid(cursor.getInt(cursor
					.getColumnIndex(TaskDaoFields.COL_NAME_TID)));
			taskDomain.setTitle(cursor.getString(cursor
					.getColumnIndex(TaskDaoFields.COL_NAME_TITLE)));
			taskDomain.setText(cursor.getString(cursor
					.getColumnIndex(TaskDaoFields.COL_NAME_TEXT)));
			taskDomain.setStatus(cursor.getInt(cursor
					.getColumnIndex(TaskDaoFields.COL_NAME_STATUS)));
		}
		cursor.close();
		return taskDomain;
	}
	
	public ArrayList<TaskDomain> selectAllTask() {
		Cursor cursor = SQLiteManager.query(TABLE_NAME_TASKS, new String[] {
				TaskDaoFields.COL_NAME_TID, TaskDaoFields.COL_NAME_TITLE,
				TaskDaoFields.COL_NAME_TEXT, TaskDaoFields.COL_NAME_STATUS },
				null, null, null, null, null);
		if (null == cursor)
			return null;

		ArrayList<TaskDomain> list = new ArrayList<TaskDomain>();
		TaskDomain taskDomain = null;

		while (cursor.moveToNext()) {
			taskDomain = new TaskDomain();
			taskDomain.setTid(cursor.getInt(cursor
					.getColumnIndex(TaskDaoFields.COL_NAME_TID)));
			taskDomain.setTitle(cursor.getString(cursor
					.getColumnIndex(TaskDaoFields.COL_NAME_TITLE)));
			taskDomain.setText(cursor.getString(cursor
					.getColumnIndex(TaskDaoFields.COL_NAME_TEXT)));
			taskDomain.setStatus(cursor.getInt(cursor
					.getColumnIndex(TaskDaoFields.COL_NAME_STATUS)));
			list.add(taskDomain);
		}
		cursor.close();
		return list;
	}

	public long insertTask(String title, String text, int status) {
		ContentValues values = new ContentValues();
		values.put(TaskDaoFields.COL_NAME_TITLE, title);
		values.put(TaskDaoFields.COL_NAME_TEXT, text);
		values.put(TaskDaoFields.COL_NAME_STATUS, status);
		return SQLiteManager.insert(TABLE_NAME_TASKS, null, values);
	}

	public int updateTask(int tid, String title, String text, int status) {
		ContentValues values = new ContentValues();
		values.put(TaskDaoFields.COL_NAME_TITLE, title);
		values.put(TaskDaoFields.COL_NAME_TEXT, text);
		values.put(TaskDaoFields.COL_NAME_STATUS, status);
		return SQLiteManager.update(TABLE_NAME_TASKS, values, "tid=?",
				new String[] { String.valueOf(tid) });
	}
	
	public int updateTaskStatus(int tid, int status) {
		ContentValues values = new ContentValues();
		values.put(TaskDaoFields.COL_NAME_STATUS, status);
		return SQLiteManager.update(TABLE_NAME_TASKS, values, "tid=?",
				new String[] { String.valueOf(tid) });
	}

	public int deleteTask(int tid) {
//		Log.d("DEBUG", "TaskDao deleteTask");
		int rtn =SQLiteManager.delete(TABLE_NAME_TASKS, "tid=?",
				new String[] { String.valueOf(tid) });
//		Log.d("DEBUG", "deleteTask rtn = " + rtn);
		return rtn;
	}
	
	public Cursor selectAllOrders() {
		 return SQLiteManager.query(TABLE_NAME_ORDERS,
				new String[] { TaskDaoFields.COL_NAME_TID }, null, null, null,
				null, null);
	}
	
	public void insertAllOrders(ArrayList<TaskDomain> dataList){
		TaskDomain task = null;
		for(int i = 0; i < dataList.size(); ++i)
		{
			task = dataList.get(i);
			if(task.getStatus() == TaskDomain.TaskStatus.NEW)
				continue;
			insertOrder(task.getTid());
		}
	}
	
	public long insertOrder(int tid){
		ContentValues values = new ContentValues();
		values.put(TaskDaoFields.COL_NAME_TID, tid);
		return SQLiteManager.insert(TABLE_NAME_ORDERS, null, values);
	}
	
	public int deleteOrder(int tid) {
//		Log.d("DEBUG", "TaskDao deleteOrder");
		int rtn = SQLiteManager.delete(TABLE_NAME_ORDERS, "tid=?",
				new String[] { String.valueOf(tid) });
//		Log.d("DEBUG", "deleteOrder = " + rtn);
		return rtn;
	}
	
}
